#include "app_frame.h"
#include "../core/graphic/texture.h"
#include "../core/graphic/render_item.h"
#include "../app_frame/font_manager.h"
#include "../core/audio/audio.h"

const char* __getDebugStatusString();

class KAppFrameUnitTest : public KAppFrame
{
public:
	KAppFrameUnitTest() {}
	virtual ~KAppFrameUnitTest() {}

	virtual bool Init();
	virtual void Destory();
	virtual void TimeElapse(unsigned int ms);
	virtual bool Render();

protected:

	KTexture* mpPngTexture;
	KRenderState mRenderState;
	std::vector<VERTEX_PT> mDrawData;

	KTexture* mpPngTexture1;
	KRenderState mRenderState1;
	std::vector<VERTEX_PT> mDrawData1;
	HW_BufferPtr mHWDrawData1;

	KAudio::SOUND_HANDLE mTestSound;
};

static KAppFrameUnitTest g_unitTest;

KAppFrame* KAppFrame::GetInstance()
{
	return &g_unitTest;
}


bool KAppFrameUnitTest::Init()
{
	if (!KAppFrame::Init())
		return false;

	mTestSound = KAudio::OpenSound("AK47B.WAV");
	if (mTestSound)
		KAudio::PlaySound(mTestSound);
	//KAudio::PlayStream("test.mp3");

	mpPngTexture = new KTexture();
	mpPngTexture->CreateFromFile("pngtest.png", true);
	BYTE* tempData = new BYTE[32*32];
	for (int i = 0; i < 32*32; ++i)
		tempData[i] = 0xff;
	mpPngTexture->UpdateRect(60, 60, 32, 32, tempData);
	delete tempData;

	mRenderState.mBlendMode = KRenderState::eAlphaBlend;
	mRenderState.mTexture = mpPngTexture;
	mRenderState.mModulateColor = 0xffffffff;

	mDrawData.resize(4);
	SET_VERTEX_POS(mDrawData[0], 0.0f, 0.0f);
	mDrawData[0].uv[0] = 0.0f;
	mDrawData[0].uv[1] = 0.0f;

	SET_VERTEX_POS(mDrawData[1], 0.0f, 150.0f);
	mDrawData[1].uv[0] = 0.0f;
	mDrawData[1].uv[1] = 1.0f;

	SET_VERTEX_POS(mDrawData[2], 150.0f, 0.0f);
	mDrawData[2].uv[0] = 1.0f;
	mDrawData[2].uv[1] = 0.0f;

	SET_VERTEX_POS(mDrawData[3], 150.0f, 150.0f);
	mDrawData[3].uv[0] = 1.0f;
	mDrawData[3].uv[1] = 1.0f;


	mpPngTexture1 = new KTexture();
	mpPngTexture1->CreateFromFile("pngtest.png", false);
	KColor* tempData1 = new KColor[32*32];
	for (int i = 0; i < 32*32; ++i)
		tempData1[i] = 0xff00ffff;
	mpPngTexture1->UpdateRect(30, 60, 32, 32, tempData1);
	delete tempData1;

	mRenderState1.mBlendMode = KRenderState::eAlphaBlend;
	mRenderState1.mTexture = mpPngTexture1;
	mRenderState1.mModulateColor = 0x6fffffff;


	mDrawData1.resize(4);
	{
		SET_VERTEX_POS(mDrawData1[0], 0.0f, 0.0f);
		mDrawData1[0].uv[0] = 0.0f;
		mDrawData1[0].uv[1] = 0.0f;

		SET_VERTEX_POS(mDrawData1[1], 0.0f, 150.0f);
		mDrawData1[1].uv[0] = 0.0f;
		mDrawData1[1].uv[1] = 1.0f;

		SET_VERTEX_POS(mDrawData1[2], 150.0f, 0.0f);
		mDrawData1[2].uv[0] = 1.0f;
		mDrawData1[2].uv[1] = 0.0f;

		SET_VERTEX_POS(mDrawData1[3], 150.0f, 150.0f);
		mDrawData1[3].uv[0] = 1.0f;
		mDrawData1[3].uv[1] = 1.0f;

		mHWDrawData1 = KDrawManager::CreateHWBuffer(&mDrawData1[0], eTriangleStrip, 4);
	}

	return true;
}

void KAppFrameUnitTest::Destory()
{
	if (mpPngTexture)
		delete mpPngTexture;

	if (mpPngTexture1)
		delete mpPngTexture1;

	KDrawManager::DeleteHWBuffer(mHWDrawData1);

	KAppFrame::Destory();
}

void KAppFrameUnitTest::TimeElapse(unsigned int ms)
{
	if (ms > MAX_FRAME_TIME)
		ms = MAX_FRAME_TIME;
	KAppFrame::TimeElapse(ms);
}

bool KAppFrameUnitTest::Render()
{

	KDrawManager::Begin();

	KDrawLocal local;

	local.x = 40;  local.y = 40;
	local.rot.SetDegree(20);
	KDrawManager::SetRenderState(mRenderState);
	KDrawManager::Draw_PT(&mDrawData[0], local, eTriangleStrip, 4);

	local.x = 140;  local.y = 40;
	local.rot.SetDegree(0);
	KDrawManager::SetRenderState(mRenderState1);
	KDrawManager::Draw_HWBuf(local, mHWDrawData1);
	//KDrawManager::Draw_PT(&mDrawData1[0], local, eTriangleStrip, 4);
	
	std::vector<VERTEX_PC> immediateData(3);
	SET_VERTEX_POS(immediateData[0], 150, 150);
	immediateData[0].color = MAKE_RGBA(0, 255, 0, 255);
	SET_VERTEX_POS(immediateData[1], 150, 190);
	immediateData[1].color = MAKE_RGBA(255, 0, 0, 255);
	SET_VERTEX_POS(immediateData[2], 190, 150);
	immediateData[2].color = MAKE_RGBA(0, 0, 255, 255);

	KDrawManager::Draw_PC(&immediateData[0], local, eTriangleStrip, 3);

	EnableCanvasView(false);
	local.x = 100;  local.y = 108;
	local.rot.SetDegree(0);
	KFontManager::GetInstance()->TextOut("I'm YOUR DAD, hahaha.....//++\\||", local);
	local.x = 100;  local.y = 60;
	KFontManager::GetInstance()->TextOut(__getDebugStatusString(), local);

	char fps_str[40];
	sprintf(fps_str, "FPS: %.2f", mFPS);
	local.x = 390;  local.y = 10;
	local.rot.SetDegree(0);
	KFontManager::GetInstance()->TextOut(fps_str, local);
	KDrawManager::End();

	// Read the touch event and play a sound
	if (mTouchEvents.GetEventCount() > 0) {
		if (mTestSound)
			KAudio::PlaySound(mTestSound);
	}


	return true;
}
